import Vue from 'vue'
import { login, getMenuByUser } from "@/api/user"
import { ACCESS_TOKEN, USER_NAME, USER_INFO, USER_ID, ORG_ID, ORG_NAME, CACHE_SELECT_SIDEMENU, CACHE_SELECT_HEADER, CACHE_SELECT_TABS, CITY_ID } from "@/store/mutation-types"

const user = {
  state: {
    token: '',
    username: '',
    avatar: '',
    menus: [],
    cityId: '',
    info: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, username) => {
      state.username = username
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_MENUS: (state, menuList) => {
      state.menus = menuList
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
    SET_CITY_ID: (state, cityId) => {
      state.cityId = cityId
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          if (response.data.code == 0) {
            const result = response.data.data
            // 保存时间
            let day = 1
            Vue.ls.set(USER_ID, result.user.id, day * 24 * 60 * 60 * 1000);
            Vue.ls.set(USER_NAME, result.user.username, day * 24 * 60 * 60 * 1000);
            Vue.ls.set(ORG_ID, result.user.orgId, day * 24 * 60 * 60 * 1000);
            Vue.ls.set(ORG_NAME, result.user.orgName, day * 24 * 60 * 60 * 1000);
            Vue.ls.set(ACCESS_TOKEN, result.token, day * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_INFO, result.user, day * 24 * 60 * 60 * 1000)
            Vue.ls.set(CITY_ID, result.user.cityId, day * 24 * 60 * 60 * 1000)
            commit('SET_NAME', result.user.username)
            commit('SET_TOKEN', result.token)
            commit('SET_CITY_ID', result.user.cityId)
            commit('SET_INFO', result.user)
            resolve(response)
          } else {
            resolve(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取菜单
    GetMenuList({ commit }) {
      return new Promise((resolve, reject) => {
        getMenuByUser({}).then(response => {
          if (response.data.code == 0) {
            const mockMenuData = [
              {
                children: [
                  { icon: "xtgl", resourceName: "个人信息", resourceCode: "xtgl", resourceUrl: "/personal/information" },
                  { icon: "xtgl", resourceName: "充值记录", resourceCode: "xtgl", resourceUrl: "/personal/rechargeRecord" }
                ],
                hasChildren: false,
                icon: "xtgl",
                id: "1697173325546680321",
                idPath: "1697173325546680321",
                orderNo: 1,
                parentId: "-1",
                parentName: null,
                resourceCode: "xtgl",
                resourceModule: "/",
                resourceName: "个人中心",
                resourceType: "0",
                resourceUrl: "/system"
              }
            ]

            // commit('SET_MENUS', response.data.data)
            commit('SET_MENUS', mockMenuData)

          } else {
            reject('获取权限菜单失败')
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout({ commit }) {
      return new Promise((resolve) => {
        commit('SET_TOKEN', '')
        commit('SET_INFO', [])
        commit('SET_MENUS', [])
        commit('SET_CITY_ID', '')
        Vue.ls.remove(USER_ID)
        Vue.ls.remove(USER_NAME)
        Vue.ls.remove(ORG_ID)
        Vue.ls.remove(ORG_NAME)
        Vue.ls.remove(CITY_ID)
        Vue.ls.remove(ACCESS_TOKEN)
        Vue.ls.remove(USER_INFO)
        Vue.ls.remove(CACHE_SELECT_SIDEMENU)
        Vue.ls.remove(CACHE_SELECT_HEADER)
        Vue.ls.remove(CACHE_SELECT_TABS)
        resolve()
      })
    }
  }
}

export default user